home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / othergnu / gnuchess.zoo / GNUCHESS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-09  |  63.4 KB  |  2,335 lines

  1. /*
  2.   C source for CHESS.
  3.  
  4.   Revision: 5-23-88
  5.  
  6.   Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  7.   Copyright (c) 1988  John Stanback
  8.  
  9.   This file is part of CHESS.
  10.  
  11.   CHESS is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY.  No author or distributor
  13.   accepts responsibility to anyone for the consequences of using it
  14.   or for whether it serves any particular purpose or works at all,
  15.   unless he says so in writing.  Refer to the CHESS General Public
  16.   License for full details.
  17.  
  18.   Everyone is granted permission to copy, modify and redistribute
  19.   CHESS, but only under the conditions described in the
  20.   CHESS General Public License.   A copy of this license is
  21.   supposed to have been given to you along with CHESS so you
  22.   can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice
  24.   and this notice must be preserved on all copies.
  25. */
  26.  
  27. /* modified to compile under Megamax Laser C by Eric R. Smith */
  28. /* Aug. 9, 1988 */
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32.  
  33. #ifdef MSDOS
  34. # include <stdlib.h>
  35. # include <time.h>
  36. # include <alloc.h>
  37. # define ttblsz 4096
  38. #else
  39. # ifdef MEGAMAX
  40. #  define ttblsz 4096
  41. #  define huge
  42. #  include <osbind.h>
  43.     extern long time();
  44. # else
  45. #  include <sys/param.h>
  46. #  include <sys/times.h>
  47. #  define ttblsz 16384
  48. #  define huge
  49. # endif MEGAMAX
  50. #endif MSDOS
  51.  
  52.  
  53. #define neutral 2
  54. #define white 0
  55. #define black 1 
  56. #define no_piece 0
  57. #define pawn 1
  58. #define knight 2
  59. #define bishop 3
  60. #define rook 4
  61. #define queen 5
  62. #define king 6
  63. #define valueP 100
  64. #define valueN 350
  65. #define valueB 355
  66. #define valueR 550
  67. #define valueQ 1100
  68. #define valueK 1200
  69. #define ctlP 0x4000
  70. #define ctlN 0x2800
  71. #define ctlB 0x1800
  72. #define ctlR 0x0400
  73. #define ctlQ 0x0200
  74. #define ctlK 0x0100
  75. #define ctlBQ 0x1200
  76. #define ctlRQ 0x0600
  77. #define ctlNN 0x2000
  78. #define pxx " PNBRQK"
  79. #define qxx " pnbrqk"
  80. #define rxx "12345678"
  81. #define cxx "abcdefgh"
  82. #define check 0x0001
  83. #define capture 0x0002
  84. #define draw 0x0004
  85. #define promote 0x0008
  86. #define cstlmask 0x0010
  87. #define epmask 0x0020
  88. #define exact 0x0040
  89. #define pwnthrt 0x0080
  90. #define truescore 0x0001
  91. #define lowerbound 0x0002
  92. #define upperbound 0x0004
  93. #define maxdepth 30
  94. #define true 1
  95. #define false 0
  96. #define absv(x) ((x) < 0 ? -(x) : (x))
  97. #define taxicab(a,b) (abs(column[a]-column[b]) + abs(row[a]-row[b]))
  98.  
  99. struct leaf
  100.   {
  101.     short f,t,score,reply;
  102.     unsigned short flags;
  103.   };
  104. struct GameRec
  105.   {
  106.     unsigned short gmove;
  107.     short score,depth,time,piece,color;
  108.     long nodes;
  109.   };
  110. struct TimeControlRec
  111.   {
  112.     short moves[2];
  113.     long clock[2];
  114.   };
  115. struct BookEntry
  116.   {
  117.     struct BookEntry *next;
  118.     unsigned short *mv;
  119.   };
  120. struct hashval
  121.   {
  122.     unsigned long bd;
  123.     unsigned short key;
  124.   };
  125. struct hashentry
  126.   {
  127.     unsigned long hashbd;
  128.     unsigned short mv,flags;
  129.     short score,depth;
  130.   };
  131.  
  132. char mvstr1[5],mvstr2[5],mvstr3[6];
  133. struct leaf Tree[1500],*root;
  134. short TrPnt[maxdepth],board[64],color[64];
  135. short row[64],column[64],locn[8][8],Pindex[64],svalue[64];
  136. short PieceList[2][16],PieceCnt[2],atak[2][64],PawnCnt[2][8];
  137. short castld[2],kingmoved[2],mtl[2],pmtl[2],emtl[2],hung[2];
  138. short c1,c2,*atk1,*atk2,*PC1,*PC2,EnemyKing;
  139. short mate,post,opponent,computer,Sdepth,Awindow,Bwindow,dither;
  140. long ResponseTime,ExtraTime,Level,et,et0,time0,cputimer,ft;
  141. long NodeCnt,evrate,ETnodes,EvalNodes,HashCnt;
  142. short quit,reverse,bothsides,hashflag,InChk,player,force,easy,beep;
  143. short wking,bking,FROMsquare,TOsquare,timeout,Zscore,zwndw,xwndw,slk;
  144. short INCscore;
  145. short HasPawn[2],HasKnight[2],HasBishop[2],HasRook[2],HasQueen[2];
  146. short ChkFlag[maxdepth],CptrFlag[maxdepth],PawnThreat[maxdepth];
  147. short Pscore[maxdepth],Tscore[maxdepth],Threat[maxdepth];
  148. struct GameRec GameList[240];
  149. short GameCnt,Game50,epsquare,lpost,rcptr,contempt;
  150. short MaxSearchDepth,Xscore;
  151. struct BookEntry *Book;
  152. struct TimeControlRec TimeControl;
  153. short TCflag,TCmoves,TCminutes,OperatorTime;
  154. short otherside[3]={1,0,2};
  155. short rank7[3]={6,1,0};
  156. short map[64]=
  157.    {0,1,2,3,4,5,6,7,
  158.     0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
  159.     0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
  160.     0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
  161.     0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
  162.     0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
  163.     0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
  164.     0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77};
  165. short unmap[120]=
  166.    {0,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1,
  167.     8,9,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,
  168.     16,17,18,19,20,21,22,23,-1,-1,-1,-1,-1,-1,-1,-1,
  169.     24,25,26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1,
  170.     32,33,34,35,36,37,38,39,-1,-1,-1,-1,-1,-1,-1,-1,
  171.     40,41,42,43,44,45,46,47,-1,-1,-1,-1,-1,-1,-1,-1,
  172.     48,49,50,51,52,53,54,55,-1,-1,-1,-1,-1,-1,-1,-1,
  173.     56,57,58,59,60,61,62,63};
  174. short Dcode[120]= 
  175.    {0,1,1,1,1,1,1,1,0,0,0,0,0,0,0x0E,0x0F,
  176.     0x10,0x11,0x12,0,0,0,0,0,0,0,0,0,0,0,0x0F,0x1F,
  177.     0x10,0x21,0x11,0,0,0,0,0,0,0,0,0,0,0x0F,0,0,
  178.     0x10,0,0,0x11,0,0,0,0,0,0,0,0,0x0F,0,0,0,
  179.     0x10,0,0,0,0x11,0,0,0,0,0,0,0x0F,0,0,0,0,
  180.     0x10,0,0,0,0,0x11,0,0,0,0,0x0F,0,0,0,0,0,
  181.     0x10,0,0,0,0,0,0x11,0,0,0x0F,0,0,0,0,0,0,
  182.     0x10,0,0,0,0,0,0,0x11};
  183. short Stboard[64]=
  184.    {rook,knight,bishop,queen,king,bishop,knight,rook,
  185.     pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
  186.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  187.     pawn,pawn,pawn,pawn,pawn,pawn,pawn,pawn,
  188.     rook,knight,bishop,queen,king,bishop,knight,rook};
  189. short Stcolor[64]=
  190.    {white,white,white,white,white,white,white,white,
  191.     white,white,white,white,white,white,white,white,
  192.     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  193.     black,black,black,black,black,black,black,black,
  194.     black,black,black,black,black,black,black,black};
  195. short sweep[7]= {false,false,false,true,true,true,false};
  196. short Dpwn[3]={4,6,0};
  197. short Dstart[7]={6,4,8,4,0,0,0};
  198. short Dstop[7]={7,5,15,7,3,7,7};
  199. short Dir[16]={1,0x10,-1,-0x10,0x0F,0x11,-0x0F,-0x11,
  200.                0x0E,-0x0E,0x12,-0x12,0x1F,-0x1F,0x21,-0x21};
  201. short Pdir[34]={0,0x38,0,0,0,0,0,0,0,0,0,0,0,0,0x02,0x35,
  202.                 0x38,0x35,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0x02,
  203.                 0,0x02};
  204. short pbit[7]={0,0x01,0x02,0x04,0x08,0x10,0x20};
  205. unsigned short killr0[maxdepth],killr1[maxdepth],killr2[maxdepth];
  206. unsigned short killr3[maxdepth],PrVar[maxdepth];
  207. unsigned short PV,hint,Swag0,Swag1,Swag2,Swag3,Swag4;
  208. unsigned short hashkey;
  209. unsigned long hashbd;
  210. struct hashval hashcode[2][7][64];
  211. struct hashentry huge *ttable,*ptbl;
  212. unsigned char history[8192];
  213.  
  214. short Mwpawn[64],Mbpawn[64],Mknight[2][64],Mbishop[2][64];
  215. short Mking[2][64],Kfield[2][64];
  216. short value[7]={0,valueP,valueN,valueB,valueR,valueQ,valueK};
  217. short control[7]={0,ctlP,ctlN,ctlB,ctlR,ctlQ,ctlK};
  218. short PassedPawn0[8]={0,60,80,120,200,360,600,800};
  219. short PassedPawn1[8]={0,30,40,60,100,180,300,800};
  220. short PassedPawn2[8]={0,15,25,35,50,90,140,800};
  221. short PassedPawn3[8]={0,5,10,15,20,30,140,800};
  222. short ISOLANI[8] = {-12,-16,-20,-24,-24,-20,-16,-12};
  223. short BACKWARD[8] = {-6,-10,-15,-21,-28,-28,-28,-28};
  224. short BMBLTY[14] = {-2,0,2,4,6,8,10,12,13,14,15,16,16,16};
  225. short RMBLTY[14] = {0,2,4,6,8,10,11,12,13,14,14,14,14,14};
  226. short Kthreat[16] = {0,-8,-20,-36,-52,-68,-80,-80,-80,-80,-80,-80,
  227.                      -80,-80,-80,-80};
  228. short KNIGHTPOST,KNIGHTSTRONG,BISHOPSTRONG,KATAK,KBNKsq;
  229. short PEDRNK2B,PWEAKH,PADVNCM,PADVNCI,PAWNSHIELD,PDOUBLED,PBLOK;
  230. short RHOPN,RHOPNX,KHOPN,KHOPNX,KSFTY;
  231. short ATAKD,HUNGP,HUNGX,KCASTLD,KMOVD,XRAY,PINVAL;
  232. short stage,stage2,Zwmtl,Zbmtl,Developed[2],PawnStorm;
  233. short PawnBonus,BishopBonus,RookBonus;
  234. short KingOpening[64]=
  235.    {  0,  0, -4,-10,-10, -4,  0,  0,
  236.      -4, -4, -8,-12,-12, -8, -4, -4,
  237.     -12,-16,-20,-20,-20,-20,-16,-12,
  238.     -16,-20,-24,-24,-24,-24,-20,-16,
  239.     -16,-20,-24,-24,-24,-24,-20,-16,
  240.     -12,-16,-20,-20,-20,-20,-16,-12,
  241.      -4, -4, -8,-12,-12, -8, -4, -4,
  242.       0,  0, -4,-10,-10, -4,  0,  0};
  243. short KingEnding[64]=
  244.    { 0, 4, 8,12,12, 8, 4, 0,
  245.      4,16,20,24,24,20,16, 4,
  246.      8,20,28,32,32,28,20, 8,
  247.     12,24,32,36,36,32,24,12,
  248.     12,24,32,36,36,32,24,12,
  249.      8,20,28,32,32,28,20, 8,
  250.      4,16,20,24,24,20,16, 4,
  251.      0, 4, 8,12,12, 8, 4, 0};
  252. short KBNK[64]=
  253.    {99,90,80,70,60,50,40,40,
  254.     90,80,60,50,40,30,20,40,
  255.     80,60,40,30,20,10,30,50,
  256.     70,50,30,10, 0,20,40,60,
  257.     60,40,20, 0,10,30,50,70,
  258.     50,30,10,20,30,40,60,80,
  259.     40,20,30,40,50,60,80,90,
  260.     40,40,50,60,70,80,90,99};
  261. short pknight[64]=
  262.    { 0, 4, 8,10,10, 8, 4, 0,
  263.      4, 8,16,20,20,16, 8, 4,
  264.      8,16,24,28,28,24,16, 8,
  265.     10,20,28,32,32,28,20,10,
  266.     10,20,28,32,32,28,20,10,
  267.      8,16,24,28,28,24,16, 8,
  268.      4, 8,16,20,20,16, 8, 4,
  269.      0, 4, 8,10,10, 8, 4, 0};
  270. short pbishop[64]=
  271.    {14,14,14,14,14,14,14,14,
  272.     14,22,18,18,18,18,22,14,
  273.     14,18,22,22,22,22,18,14,
  274.     14,18,22,22,22,22,18,14,
  275.     14,18,22,22,22,22,18,14,
  276.     14,18,22,22,22,22,18,14,
  277.     14,22,18,18,18,18,22,14,
  278.     14,14,14,14,14,14,14,14};
  279. short PawnAdvance[64]=
  280.    { 0, 0, 0, 0, 0, 0, 0, 0,
  281.      4, 4, 4, 0, 0, 4, 4, 4,
  282.      6, 8, 2,10,10, 2, 8, 6,
  283.      6, 8,12,16,16,12, 8, 6,
  284.      8,12,16,24,24,16,12, 8,
  285.     12,16,24,32,32,24,16,12,
  286.     12,16,24,32,32,24,16,12,
  287.      0, 0, 0, 0, 0, 0, 0, 0};
  288.      
  289.      
  290. main(argc,argv)
  291. int argc; char *argv[];
  292. {
  293. #ifdef MSDOS
  294.   ttable = (struct hashentry huge *)farmalloc(ttblsz *
  295.            (unsigned long)sizeof(struct hashentry));
  296. #else
  297.   ttable = (struct hashentry *)lmalloc(ttblsz *
  298.            (unsigned long)sizeof(struct hashentry));
  299. #endif
  300.   Level = 0; TCflag = false; OperatorTime = 0;
  301.   if (argc == 2) Level = atoi(argv[1]);
  302.   if (argc == 3)
  303.     {
  304.       TCmoves = atoi(argv[1]); TCminutes = atoi(argv[2]); TCflag = true;
  305.     }
  306.   Initialize();
  307.   NewGame();
  308.   while (!(quit))
  309.     {
  310.       if (bothsides && !mate) SelectMove(opponent,1); else InputCommand();
  311.       if (!(quit || mate || force)) SelectMove(computer,1);
  312.     }
  313.   ExitChess();
  314. }
  315.  
  316.  
  317.  
  318. /* ............    INTERFACE ROUTINES    ........................... */
  319.  
  320. int VerifyMove(s,iop,mv)
  321. char s[];
  322. short iop;
  323. unsigned short *mv;
  324.  
  325. /*
  326.    Compare the string 's' to the list of legal moves available for the 
  327.    opponent. If a match is found, make the move on the board. 
  328. */
  329.  
  330. {
  331. static short pnt,tempb,tempc,tempsf,tempst,cnt;
  332. static struct leaf xnode;
  333. struct leaf *node;
  334.  
  335.   *mv = 0;
  336.   if (iop == 2)
  337.     {
  338.       UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  339.       return(false);
  340.     }
  341.   cnt = 0;
  342.   MoveList(opponent,2);
  343.   pnt = TrPnt[2];
  344.   while (pnt < TrPnt[3])
  345.     {
  346.       node = &Tree[pnt++];
  347.       algbr(node->f,node->t,node->flags & cstlmask);
  348.       if (strcmp(s,mvstr1) == 0 || strcmp(s,mvstr2) == 0 ||
  349.           strcmp(s,mvstr3) == 0)
  350.         {
  351.           cnt++; xnode = *node;
  352.         }
  353.     }
  354.   if (cnt == 1)
  355.     {
  356.       MakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  357.       if (SqAtakd(PieceList[opponent][0],computer))
  358.         {
  359.           UnmakeMove(opponent,&xnode,&tempb,&tempc,&tempsf,&tempst);
  360.           ShowMessage("Illegal Move!!");
  361.           return(false);
  362.         }
  363.       else
  364.         {
  365.           if (iop == 1) return(true);
  366.           if (xnode.flags & epmask) UpdateDisplay(0,0,1,0);
  367.           else UpdateDisplay(xnode.f,xnode.t,0,xnode.flags & cstlmask);
  368.           if (xnode.flags & cstlmask) Game50 = GameCnt;
  369.           else if (board[xnode.t] == pawn || (xnode.flags & capture)) 
  370.             Game50 = GameCnt;
  371.           GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  372.           GameList[GameCnt].nodes = 0;
  373.           ElapsedTime(1);
  374.           GameList[GameCnt].time = (short)et;
  375.           TimeControl.clock[opponent] -= et;
  376.           --TimeControl.moves[opponent];
  377.           *mv = (xnode.f << 8) + xnode.t;
  378.           algbr(xnode.f,xnode.t,false);
  379.           return(true);
  380.         } 
  381.     }
  382.   if (cnt > 1) ShowMessage("Ambiguous Move!");
  383.   return(false);
  384. }
  385.  
  386.  
  387. NewGame()
  388.  
  389. /*
  390.    Reset the board and other variables to start a new game.
  391. */
  392.  
  393. {
  394. short l,r,c,p;
  395.  
  396.   mate = quit = reverse = bothsides = post = false;
  397.   hashflag = force = PawnStorm = false;
  398.   easy = beep = rcptr = true;
  399.   lpost =  NodeCnt = epsquare = et0 = 0;
  400.   dither = 0;
  401.   Awindow = 90;
  402.   Bwindow = 90;
  403.   xwndw = 90;
  404.   MaxSearchDepth = 29;
  405.   contempt = 0;
  406.   GameCnt = -1; Game50 = 0;
  407.   Zwmtl = Zbmtl = 0;
  408.   Developed[white] = Developed[black] = false;
  409.   castld[white] = castld[black] = false;
  410.   kingmoved[white] = kingmoved[black] = 0;
  411.   PawnThreat[0] = CptrFlag[0] = Threat[0] = false;
  412.   Pscore[0] = 12000; Tscore[0] = 12000;
  413.   opponent = white; computer = black;
  414.   for (r = 0; r < 8; r++)
  415.     for (c = 0; c < 8; c++)
  416.       {
  417.         l = 8*r+c; locn[r][c] = l;
  418.         row[l] = r; column[l] = c;
  419.         board[l] = Stboard[l]; color[l] = Stcolor[l];
  420.       }
  421.   for (c = white; c <= black; c++)
  422.     for (p = pawn; p <= king; p++)
  423.       for (l = 0; l < 64; l++)
  424.         {
  425.           hashcode[c][p][l].key = (unsigned short)rand();
  426.           hashcode[c][p][l].bd = ((unsigned long)rand() << 16) +
  427.                                  (unsigned long)rand();
  428.         }
  429.   ClrScreen();
  430.   if (TCflag) SetTimeControl();
  431.   else if (Level == 0) SelectLevel();
  432.   UpdateDisplay(0,0,1,0);
  433.   InitializeStats();
  434.   time0 = time((long *)0);
  435.   ElapsedTime(1);
  436.   GetOpenings();
  437. }
  438.  
  439.  
  440. algbr(f,t,flag)
  441. short f,t,flag;
  442. {
  443.   mvstr1[0] = cxx[column[f]]; mvstr1[1] = rxx[row[f]];
  444.   mvstr1[2] = cxx[column[t]]; mvstr1[3] = rxx[row[t]];
  445.   mvstr2[0] = qxx[board[f]];
  446.   mvstr2[1] = mvstr1[2]; mvstr2[2] = mvstr1[3];
  447.   mvstr1[4] = '\0'; mvstr2[3] = '\0';
  448.   if (flag)
  449.     if (t > f) strcpy(mvstr2,"o-o");
  450.     else strcpy(mvstr2,"o-o-o");
  451.  
  452.   if (board[f] == pawn) mvstr3[0] = mvstr1[0];
  453.   else mvstr3[0] = qxx[board[f]];
  454.   if (color[t] != neutral)
  455.     {
  456.       mvstr3[1] = ':';
  457.       mvstr3[2] = mvstr1[2];
  458.       mvstr3[3] = mvstr1[3];
  459.       mvstr3[4] = '\0';
  460.     }
  461.   else if (board[f] == pawn)
  462.     {
  463.       mvstr3[1] = mvstr1[3];
  464.       mvstr3[2] = '\0';
  465.     }
  466.   else
  467.     {
  468.       mvstr3[1] = mvstr1[0];
  469.       mvstr3[2] = mvstr1[1];
  470.       mvstr3[3] = mvstr1[2];
  471.       mvstr3[4] = mvstr1[3];
  472.       mvstr3[5] = '\0';
  473.     }
  474. }
  475.  
  476.  
  477. /* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */
  478.  
  479. SelectMove(side,iop)
  480. short side,iop;
  481.  
  482. /*
  483.    Select a move by calling function search() at progressively deeper 
  484.    ply until time is up or a mate or draw is reached. An alpha-beta 
  485.    window of -90 to +90 points is set around the score returned from the 
  486.    previous iteration. If Sdepth != 0 then the program has correctly 
  487.    predicted the opponents move and the search will start at a depth of 
  488.    Sdepth+1 rather than a depth of 1. 
  489. */
  490.  
  491. {
  492. static short i,alpha,beta,score,tempb,tempc,tempsf,tempst,xside,rpt;
  493.  
  494.   timeout = false;
  495.   xside = otherside[side];
  496.   if (iop != 2) player = side;
  497.   if (TCflag)
  498.     {
  499.       ResponseTime = (TimeControl.clock[side]) /
  500.                      (TimeControl.moves[side] + 3) -
  501.                      OperatorTime;
  502.       ResponseTime += (ResponseTime*TimeControl.moves[side])/(2*TCmoves+1);
  503.     }
  504.   else ResponseTime = Level;
  505.   if (iop == 2) ResponseTime = 999;
  506.   if (Sdepth > 0 && root->score > Zscore-zwndw) ResponseTime -= ft;
  507.   else if (ResponseTime < 1) ResponseTime = 1;
  508.   ExtraTime = 0;
  509.   ExaminePosition();
  510.   ScorePosition(side,&score);
  511.   Pscore[0] = -score;
  512.   ShowSidetomove();
  513.   
  514.   if (Sdepth == 0)
  515.   {
  516.     ZeroTTable();
  517.     SearchStartStuff(side);
  518.     for (i = 0; i < 8192; i++) history[i] = 0;
  519.     FROMsquare = TOsquare = -1;
  520.     PV = 0;
  521.     if (iop != 2) hint = 0;
  522.     for (i = 0; i < maxdepth; i++)
  523.      PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;
  524.      
  525.     alpha = -9000; beta = 9000;
  526.     rpt = 0;
  527.     TrPnt[1] = 0; root = &Tree[0];
  528.     MoveList(side,1);
  529.     for (i = TrPnt[1]; i < TrPnt[2]; i++) pick(i,TrPnt[2]-1);
  530.     if (Book != NULL) OpeningBook();
  531.     if (Book != NULL) timeout = true;
  532.     NodeCnt = ETnodes = EvalNodes = HashCnt = 0;
  533.     Zscore = 0; zwndw = 20;
  534.   }
  535.   
  536.   while (!timeout && Sdepth < MaxSearchDepth)
  537.     {
  538.       Sdepth++;
  539.       ShowDepth(' ');
  540.       score = search(side,1,Sdepth,alpha,beta,PrVar,&rpt);
  541.       for (i = 1; i <= Sdepth; i++) killr0[i] = PrVar[i];
  542.       if (score < alpha && !timeout)
  543.         {
  544.           ShowDepth('-');
  545.           ExtraTime = 10*ResponseTime;
  546.           ZeroTTable();
  547.           score = search(side,1,Sdepth,-9000,beta,PrVar,&rpt);
  548.         }
  549.       if (score > beta && !timeout && !(root->flags & exact))
  550.         {
  551.           ShowDepth('+');
  552.           ExtraTime = 0;
  553.           ZeroTTable();
  554.           score = search(side,1,Sdepth,alpha,9000,PrVar,&rpt);
  555.         }
  556.       score = root->score;
  557.       if (!timeout)
  558.         for (i = TrPnt[1]+1; i < TrPnt[2]; i++) pick(i,TrPnt[2]-1);
  559.       ShowResults(score,PrVar,'.');
  560.       for (i = 1; i <= Sdepth; i++) killr0[i] = PrVar[i];
  561.       if (score > Zscore-zwndw && score > Tree[1].score+250) ExtraTime = 0;
  562.       else if (score > Zscore-3*zwndw) ExtraTime = ResponseTime;
  563.       else ExtraTime = 3*ResponseTime;
  564.       if (root->flags & exact) timeout = true;
  565.       if (Tree[1].score < -9000) timeout = true;
  566.       if (4*et > 2*ResponseTime + ExtraTime) timeout = true;
  567.       if (!timeout)
  568.         {
  569.           Tscore[0] = score;
  570.           if (Zscore == 0) Zscore = score;
  571.           else Zscore = (Zscore+score)/2;
  572.         }
  573.       zwndw = 20+abs(Zscore/12);
  574.       beta = score + Bwindow;
  575.       if (Zscore < score) alpha = Zscore - Awindow - zwndw;
  576.       else alpha = score - Awindow - zwndw;
  577.     }
  578.  
  579.   score = root->score;
  580.   if (rpt >= 2 || score < -12000) root->flags |= draw;
  581.   if (iop == 2) return(0);
  582.   if (Book == NULL) hint = PrVar[2];
  583.   ElapsedTime(1);
  584.  
  585.   if (score > -9999 && rpt <= 2)
  586.     {
  587.       MakeMove(side,root,&tempb,&tempc,&tempsf,&tempst);
  588.       algbr(root->f,root->t,root->flags & cstlmask);
  589.     }
  590.   else mvstr1[0] = '\0';
  591.   OutputMove();
  592.   if (score == -9999 || score == 9998) mate = true;
  593.   if (mate) hint = 0;
  594.   if (root->flags & cstlmask) Game50 = GameCnt;
  595.   else if (board[root->t] == pawn || (root->flags & capture)) 
  596.     Game50 = GameCnt;
  597.   GameList[GameCnt].score = score;
  598.   GameList[GameCnt].nodes = NodeCnt;
  599.   GameList[GameCnt].time = (short)et;
  600.   GameList[GameCnt].depth = Sdepth;
  601.   if (TCflag)
  602.     {
  603.       TimeControl.clock[side] -= (et + OperatorTime);
  604.       if (--TimeControl.moves[side] == 0) SetTimeControl();
  605.     }
  606.   if ((root->flags & draw) && bothsides) quit = true;
  607.   if (GameCnt > 238) quit = true;
  608.   player = xside;
  609.   Sdepth = 0;
  610.   fflush(stdin);
  611.   return(0);
  612. }
  613.  
  614. OpeningBook()
  615.  
  616. /*
  617.    Go thru each of the opening lines of play and check for a match with 
  618.    the current game listing. If a match occurs, generate a random number. 
  619.    If this number is the largest generated so far then the next move in 
  620.    this line becomes the current "candidate". After all lines are 
  621.    checked, the candidate move is put at the top of the Tree[] array and 
  622.    will be played by the program. Note that the program does not handle 
  623.    book transpositions. 
  624. */
  625.  
  626. {
  627. short j,pnt;
  628. unsigned short m,*mp;
  629. unsigned r,r0;
  630. struct BookEntry *p;
  631.  
  632.   srand((unsigned)time0);
  633.   r0 = m = 0;
  634.   p = Book;
  635.   while (p != NULL)
  636.     {
  637.       mp = p->mv;
  638.       for (j = 0; j <= GameCnt; j++)
  639.         if (GameList[j].gmove != *(mp++)) break;
  640.       if (j > GameCnt)
  641.         if ((r=rand()) > r0)
  642.           {
  643.             r0 = r; m = *mp;
  644.             hint = *(++mp);
  645.           }
  646.       p = p->next;
  647.     }
  648.     
  649.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  650.     if ((Tree[pnt].f<<8) + Tree[pnt].t == m) Tree[pnt].score = 0;
  651.   pick(TrPnt[1],TrPnt[2]-1);
  652.   if (Tree[TrPnt[1]].score < 0) Book = NULL;
  653. }
  654.  
  655.  
  656. #define UpdateSearchStatus\
  657. {\
  658.   if (post) ShowCurrentMove(pnt,node->f,node->t);\
  659.   if (pnt > TrPnt[1])\
  660.     {\
  661.       d = best-Zscore; e = best-node->score;\
  662.       if (best < alpha) ExtraTime = 10*ResponseTime;\
  663.       else if (d > -zwndw && e > 4*zwndw) ExtraTime = -ResponseTime/3;\
  664.       else if (d > -zwndw) ExtraTime = 0;\
  665.       else if (d > -3*zwndw) ExtraTime = ResponseTime;\
  666.       else if (d > -9*zwndw) ExtraTime = 3*ResponseTime;\
  667.       else ExtraTime = 5*ResponseTime;\
  668.     }\
  669. }
  670.  
  671. int search(side,ply,depth,alpha,beta,bstline,rpt)
  672. short side,ply,depth,alpha,beta,*rpt;
  673. unsigned short bstline[];
  674.  
  675. /*
  676.    Perform an alpha-beta search to determine the score for the current 
  677.    board position. If depth <= 0 only capturing moves, pawn promotions 
  678.    and responses to check are generated and searched, otherwise all 
  679.    moves are processed. The search depth is modified for check evasions, 
  680.    certain re-captures and threats. Extensions may continue for up to 11 
  681.    ply beyond the nominal search depth. 
  682. */
  683.  
  684. #define prune (cf && score+node->score < alpha)
  685. #define ReCapture (rcptr && score > alpha && score < beta &&\
  686.                    ply > 2 && CptrFlag[ply-1] && CptrFlag[ply-2] &&\
  687.                    depth == Sdepth-ply+1)
  688. #define Parry (hung[side] > 1 && ply == Sdepth+1)
  689. #define MateThreat (ply < Sdepth+4 && ply > 4 &&\
  690.                     ChkFlag[ply-2] && ChkFlag[ply-4] &&\
  691.                     ChkFlag[ply-2] != ChkFlag[ply-4])
  692.  
  693. {
  694. register short j,pnt;
  695. short best,tempb,tempc,tempsf,tempst;
  696. short xside,pbst,d,e,cf,score,rcnt;
  697. unsigned short mv,nxtline[maxdepth];
  698. struct leaf *node,tmp;
  699.  
  700.   NodeCnt++;
  701.   xside = otherside[side];
  702.   
  703.   if (ply <= Sdepth+3) repetition(rpt); else *rpt = 0;
  704.   if (*rpt >= 2) return(0);
  705.  
  706.   score = evaluate(side,xside,ply,depth,alpha,beta);
  707.   if (score > 9000) return(score);
  708.   
  709.   if (depth > 0)
  710.     {
  711.       if (InChk || PawnThreat[ply-1] || ReCapture) ++depth;
  712.     }
  713.   else
  714.     {
  715.       if (score >= alpha &&
  716.          (InChk || PawnThreat[ply-1] || Parry)) depth = 1;
  717.       else if (score <= beta && MateThreat) depth = 1;
  718.     }
  719.     
  720.   PV = 0;
  721.   if (depth > 0 && hashflag && ply > 1)
  722.     {
  723.       ProbeTTable(side,depth,&alpha,&beta,&score);
  724.       bstline[ply] = PV;
  725.       bstline[ply+1] = 0;
  726.       if (beta == -20000) return(score);
  727.       if (alpha > beta) return(alpha);
  728.     }
  729.   
  730.   if (Sdepth == 1) d = 7; else d = 11;
  731.   if (ply > Sdepth+d || (depth <= 0 && score > beta)) return(score);
  732.  
  733.   if (ply > 1)
  734.     if (depth > 0) MoveList(side,ply);
  735.     else CaptureList(side,xside,ply);
  736.     
  737.   if (TrPnt[ply] == TrPnt[ply+1]) return(score);
  738.     
  739.   cf = (depth < 1 && ply > Sdepth+1 && !ChkFlag[ply-2] && !slk);
  740.  
  741.   if (depth > 0) best = -12000; else best = score;
  742.   if (best > alpha) alpha = best;
  743.   
  744.   for (pnt = pbst = TrPnt[ply];
  745.        pnt < TrPnt[ply+1] && best <= beta;
  746.        pnt++)
  747.     {
  748.       if (ply > 1) pick(pnt,TrPnt[ply+1]-1);
  749.       node = &Tree[pnt];
  750.       mv = (node->f << 8) + node->t;
  751.       nxtline[ply+1] = 0;
  752.       
  753.       if (prune) break;
  754.       if (ply == 1) UpdateSearchStatus;
  755.  
  756.       if (!(node->flags & exact))
  757.         {
  758.           MakeMove(side,node,&tempb,&tempc,&tempsf,&tempst);
  759.           CptrFlag[ply] = (node->flags & capture);
  760.           PawnThreat[ply] = (node->flags & pwnthrt);
  761.           Tscore[ply] = node->score;
  762.           node->score = -search(xside,ply+1,depth-1,-beta,-alpha,
  763.                                 nxtline,&rcnt);
  764.           if (abs(node->score) > 9000) node->flags |= exact;
  765.           else if (rcnt == 1) node->score /= 2;
  766.           if (rcnt >= 2 || GameCnt-Game50 > 99 ||
  767.              (node->score == 9999-ply && !ChkFlag[ply]))
  768.             {
  769.               node->flags |= draw; node->flags |= exact;
  770.               if (side == computer) node->score = contempt;
  771.               else node->score = -contempt;
  772.             }
  773.           UnmakeMove(side,node,&tempb,&tempc,&tempsf,&tempst);
  774.         }
  775.       if (node->score > best && !timeout)
  776.         {
  777.           if (depth > 0)
  778.             if (node->score > alpha && !(node->flags & exact))
  779.               node->score += depth;
  780.           best = node->score; pbst = pnt;
  781.           if (best > alpha) alpha = best;
  782.           for (j = ply+1; nxtline[j] > 0; j++) bstline[j] = nxtline[j];
  783.           bstline[j] = 0;
  784.           bstline[ply] = mv;
  785.           if (ply == 1)
  786.             {
  787.               if (best == alpha)
  788.                 {
  789.                   tmp = Tree[pnt];
  790.                   for (j = pnt-1; j >= 0; j--) Tree[j+1] = Tree[j];
  791.                   Tree[0] = tmp;
  792.                   pbst = 0;
  793.                 }
  794.               if (Sdepth > 2)
  795.                 if (best > beta) ShowResults(best,bstline,'+');
  796.                 else if (best < alpha) ShowResults(best,bstline,'-');
  797.                 else ShowResults(best,bstline,'&');
  798.             }
  799.         }
  800.       if (NodeCnt > ETnodes) ElapsedTime(0);
  801.       if (timeout) return(-Tscore[ply-1]);
  802.     }
  803.     
  804.   node = &Tree[pbst];
  805.   mv = (node->f<<8) + node->t;
  806.   if (hashflag && ply <= Sdepth && *rpt == 0 && best == alpha)
  807.     PutInTTable(side,best,depth,alpha,beta,mv);
  808.   if (depth > 0)
  809.     {
  810.       j = (node->f<<6) + node->t; if (side == black) j |= 0x1000;
  811.       if (history[j] < 150) history[j] += 2*depth;
  812.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  813.         if (best <= beta) killr3[ply] = mv;
  814.         else if (mv != killr1[ply])
  815.           {
  816.             killr2[ply] = killr1[ply];
  817.             killr1[ply] = mv;
  818.           }
  819.       if (best > 9000) killr0[ply] = mv; else killr0[ply] = 0;
  820.     }
  821.   return(best);
  822. }
  823.  
  824.  
  825. int evaluate(side,xside,ply,depth,alpha,beta)
  826. short side,xside,ply,depth,alpha,beta;
  827.  
  828. /*
  829.    Compute an estimate of the score by adding the positional score from 
  830.    the previous ply to the material difference. If this score falls 
  831.    inside a window which is 180 points wider than the alpha-beta window 
  832.    (or within a 50 point window during quiescence search) call 
  833.    ScorePosition() to determine a score, otherwise return the estimated 
  834.    score. If one side has only a king and the other either has no pawns 
  835.    or no pieces then the function ScoreLoneKing() is called. 
  836. */
  837.  
  838. {
  839. register short evflag;
  840.  
  841.   Xscore = -Pscore[ply-1] - INCscore + mtl[side] - mtl[xside];
  842.   hung[white] = hung[black] = 0;
  843.   slk = ((mtl[white] == valueK && (pmtl[black] == 0 || emtl[black] == 0)) ||
  844.          (mtl[black] == valueK && (pmtl[white] == 0 || emtl[white] == 0)));
  845.   
  846.   if (slk) evflag = false;
  847.   else evflag = 
  848.      (ply == 1 || ply < Sdepth ||
  849.      (depth == 0 && Xscore > alpha-xwndw && Xscore < beta+xwndw) ||
  850.      (depth < 0 && Xscore > alpha-25 && Xscore < beta+25));
  851.     
  852.   if (evflag)
  853.     {
  854.       EvalNodes++;
  855.       ataks(side,atak[side]);
  856.       if (atak[side][PieceList[xside][0]] > 0) return(10001-ply);
  857.       ataks(xside,atak[xside]);
  858.       InChk = (atak[xside][PieceList[side][0]] > 0);
  859.       ScorePosition(side,&Xscore);
  860.     }
  861.   else
  862.     {
  863.       if (SqAtakd(PieceList[xside][0],side)) return(10001-ply);
  864.       InChk = SqAtakd(PieceList[side][0],xside);
  865.       if (slk) ScoreLoneKing(side,&Xscore);
  866.     }
  867.     
  868.   Pscore[ply] = Xscore - mtl[side] + mtl[xside];
  869.   if (InChk) ChkFlag[ply-1] = Pindex[TOsquare];
  870.   else ChkFlag[ply-1] = 0;
  871.   return(Xscore);
  872. }
  873.  
  874.  
  875. ProbeTTable(side,depth,alpha,beta,score)
  876. short side,depth,*alpha,*beta,*score;
  877.  
  878. /* 
  879.    Look for the current board position in the transposition table.
  880. */
  881.  
  882. {
  883. short hindx;
  884.   if (side == white) hashkey |= 1; else hashkey &= 0xFFFE;
  885.   hindx = (hashkey & (ttblsz-1));
  886.   ptbl = (ttable + hindx);
  887.   if (ptbl->depth >= depth && ptbl->hashbd == hashbd)
  888.     {
  889.       HashCnt++;
  890.       PV = ptbl->mv;
  891.       if (ptbl->flags & truescore)
  892.         {
  893.           *score = ptbl->score;
  894.           *beta = -20000;
  895.           return(true);
  896.         }
  897. /*
  898.       else if (ptbl->flags & upperbound)
  899.         {
  900.           if (ptbl->score < *beta) *beta = ptbl->score+1;
  901.         }
  902. */
  903.       else if (ptbl->flags & lowerbound)
  904.         {
  905.           if (ptbl->score > *alpha) *alpha = ptbl->score-1;
  906.         }
  907.     }
  908.   return(false);
  909. }
  910.  
  911.  
  912. PutInTTable(side,score,depth,alpha,beta,mv)
  913. short side,score,depth,alpha,beta;
  914. unsigned short mv;
  915.  
  916. /*
  917.    Store the current board position in the transposition table.
  918. */
  919.  
  920. {
  921. short hindx;
  922.   if (side == white) hashkey |= 1; else hashkey &= 0xFFFE;
  923.   hindx = (hashkey & (ttblsz-1));
  924.   ptbl = (ttable + hindx);
  925.   ptbl->hashbd = hashbd;
  926.   ptbl->depth = depth;
  927.   ptbl->score = score; 
  928.   ptbl->mv = mv;
  929.   ptbl->flags = 0;
  930.   if (score < alpha) ptbl->flags |= upperbound;
  931.   else if (score > beta) ptbl->flags |= lowerbound;
  932.   else ptbl->flags |= truescore;
  933. }
  934.  
  935.  
  936. ZeroTTable()
  937. {
  938. int i;
  939.   if (hashflag)
  940.     for (i = 0; i < ttblsz; i++)
  941.       {
  942.         ptbl = (ttable + i);
  943.         ptbl->depth = 0;
  944.       }
  945. }
  946.  
  947.  
  948. MoveList(side,ply)
  949. short side,ply;
  950.  
  951. /*
  952.    Fill the array Tree[] with all available moves for side to play. Array 
  953.    TrPnt[ply] contains the index into Tree[] of the first move at a ply. 
  954. */
  955.     
  956. {
  957. register short i,xside,f;
  958.  
  959.   xside = otherside[side];
  960.   if (PV == 0) Swag0 = killr0[ply]; else Swag0 = PV;
  961.   Swag1 = killr1[ply]; Swag2 = killr2[ply];
  962.   Swag3 = killr3[ply]; Swag4 = 0;
  963.   if (ply > 2) Swag4 = killr1[ply-2];
  964.   TrPnt[ply+1] = TrPnt[ply];
  965.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  966.   for (i = PieceCnt[side]; i >= 0; i--)
  967.     GenMoves(ply,PieceList[side][i],side,xside);
  968.   if (kingmoved[side] == 0 && !castld[side])
  969.     {
  970.       f = PieceList[side][0];
  971.       if (castle(side,f,f+2,0))
  972.         {
  973.           LinkMove(ply,f,f+2,xside);
  974.           Tree[TrPnt[ply+1]-1].flags |= cstlmask;
  975.         }
  976.       if (castle(side,f,f-2,0))
  977.         {
  978.           LinkMove(ply,f,f-2,xside);
  979.           Tree[TrPnt[ply+1]-1].flags |= cstlmask;
  980.         }
  981.     }
  982. }
  983.  
  984.  
  985. GenMoves(ply,sq,side,xside)
  986. short ply,sq,side,xside;
  987.  
  988. /*
  989.    Generate moves for a piece. The from square is mapped onto a special  
  990.    board and offsets (taken from array Dir[]) are added to the mapped 
  991.    location. The newly generated square is tested to see if it falls off 
  992.    the board by ANDing the square with 88 HEX. Legal moves are linked 
  993.    into the tree. 
  994. */
  995.     
  996. {
  997. register short m,u,d,i,m0,piece; 
  998.  
  999.   piece = board[sq]; m0 = map[sq];
  1000.   if (sweep[piece])
  1001.     for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  1002.       {
  1003.         d = Dir[i]; m = m0+d;
  1004.         while (!(m & 0x88))
  1005.           {
  1006.             u = unmap[m];
  1007.             if (color[u] == neutral)
  1008.               {
  1009.                 LinkMove(ply,sq,u,xside);
  1010.                 m += d;
  1011.               }
  1012.             else if (color[u] == xside)
  1013.               {
  1014.                 LinkMove(ply,sq,u,xside);
  1015.                 break;
  1016.               }
  1017.             else break;
  1018.           }
  1019.       }
  1020.   else if (piece == pawn)
  1021.     {
  1022.       if (side == white && color[sq+8] == neutral)
  1023.         {
  1024.           LinkMove(ply,sq,sq+8,xside);
  1025.           if (row[sq] == 1)
  1026.             if (color[sq+16] == neutral)
  1027.               LinkMove(ply,sq,sq+16,xside);
  1028.         }
  1029.       else if (side == black && color[sq-8] == neutral)
  1030.         {
  1031.           LinkMove(ply,sq,sq-8,xside);
  1032.           if (row[sq] == 6)
  1033.             if (color[sq-16] == neutral)
  1034.               LinkMove(ply,sq,sq-16,xside);
  1035.         }
  1036.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  1037.         if (!((m = m0+Dir[i]) & 0x88))
  1038.           {
  1039.             u = unmap[m];
  1040.             if (color[u] == xside || u == epsquare)
  1041.               LinkMove(ply,sq,u,xside);
  1042.           }
  1043.     }
  1044.   else
  1045.     {
  1046.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  1047.         if (!((m = m0+Dir[i]) & 0x88))
  1048.           {
  1049.             u = unmap[m];
  1050.             if (color[u] != side) LinkMove(ply,sq,u,xside);
  1051.           }
  1052.     }
  1053. }
  1054.  
  1055. LinkMove(ply,f,t,xside)
  1056. short ply,f,t,xside;
  1057.  
  1058. /*
  1059.    Add a move to the tree.  Assign a bonus to order the moves
  1060.    as follows:
  1061.      1. Principle variation
  1062.      2. Capture of last moved piece
  1063.      3. Other captures (major pieces first)
  1064.      4. Killer moves
  1065.      5. "history" killers    
  1066. */
  1067.  
  1068. {
  1069. register short s,z;
  1070. register unsigned short mv;
  1071. struct leaf *node;
  1072.  
  1073.   node = &Tree[TrPnt[ply+1]];
  1074.   ++TrPnt[ply+1];
  1075.   node->flags = 0;
  1076.   node->f = f; node->t = t;
  1077.   mv = (f<<8) + t;
  1078.   s = 0;
  1079.   if (mv == Swag0) s = 2000;
  1080.   else if (mv == Swag1) s = 60;
  1081.   else if (mv == Swag2) s = 50;
  1082.   else if (mv == Swag3) s = 40;
  1083.   else if (mv == Swag4) s = 30;
  1084.   if (color[t] != neutral)
  1085.     {
  1086.       node->flags |= capture;
  1087.       if (t == TOsquare) s += 500;
  1088.       s += value[board[t]] - board[f];
  1089.     }
  1090.   if (board[f] == pawn)
  1091.     if (row[t] == 0 || row[t] == 7)
  1092.       {
  1093.         node->flags |= promote;
  1094.         s += 800;
  1095.       }
  1096.     else if (row[t] == 1 || row[t] == 6)
  1097.       {
  1098.         node->flags |= pwnthrt;
  1099.         s += 600;
  1100.       }
  1101.     else if (t == epsquare) node->flags |= epmask;
  1102.   z = (f<<6) + t; if (xside == white) z |= 0x1000;
  1103.   s += history[z];
  1104.   node->score = s - 20000;
  1105. }
  1106.  
  1107.  
  1108. CaptureList(side,xside,ply)
  1109. short side,xside,ply;
  1110.  
  1111. /*
  1112.     Generate captures and Pawn promotions only.
  1113. */
  1114.  
  1115. #define LinkCapture\
  1116. {\
  1117.   node->f = sq; node->t = u;\
  1118.   node->flags = capture;\
  1119.   node->score = value[board[u]] + svalue[board[u]] - piece;\
  1120.   if (piece == pawn && (u < 8 || u > 55))\
  1121.     {\
  1122.       node->flags |= promote;\
  1123.       node->score = valueQ;\
  1124.     }\
  1125.   ++node;\
  1126.   ++TrPnt[ply+1];\
  1127. }
  1128.  
  1129. {
  1130. register short m,u,d,sq,m0;
  1131. short i,j,j1,j2,r7,d0,piece,*PL;
  1132. struct leaf *node;
  1133.  
  1134.   TrPnt[ply+1] = TrPnt[ply];
  1135.   node = &Tree[TrPnt[ply]];
  1136.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  1137.   if (side == white)
  1138.     {
  1139.       r7 = 6; d0 = 8;
  1140.     }
  1141.   else
  1142.     {
  1143.       r7 = 1; d0 = -8;
  1144.     }
  1145.   PL = PieceList[side];
  1146.   for (i = 0; i <= PieceCnt[side]; i++)
  1147.     { 
  1148.       sq = PL[i];
  1149.       m0 = map[sq]; piece = board[sq];
  1150.       j1 = Dstart[piece]; j2 = Dstop[piece];
  1151.       if (sweep[piece])
  1152.         for (j = j1; j <= j2; j++)
  1153.           {
  1154.             d = Dir[j]; m = m0+d;
  1155.             while (!(m & 0x88))
  1156.               {
  1157.                 u = unmap[m];
  1158.                 if (color[u] == neutral) m += d;
  1159.                 else
  1160.                   {
  1161.                     if (color[u] == xside) LinkCapture;
  1162.                     break;
  1163.                   }
  1164.               }
  1165.           }
  1166.       else
  1167.         {
  1168.           for (j = j1; j <= j2; j++)
  1169.             if (!((m = m0+Dir[j]) & 0x88))
  1170.               {
  1171.                 u = unmap[m];
  1172.                 if (color[u] == xside) LinkCapture;
  1173.               }
  1174.           if (piece == pawn && row[sq] == r7)
  1175.             {
  1176.               u = sq+d0;
  1177.               if (color[u] == neutral) LinkCapture;
  1178.             }
  1179.         }
  1180.     }
  1181. }
  1182.  
  1183.   
  1184. int castle(side,kf,kt,iop)
  1185. short side,kf,kt,iop;
  1186.  
  1187. /*
  1188.    Make or Unmake a castling move.
  1189. */
  1190.  
  1191. {
  1192. register short rf,rt,d,t0,xside;
  1193.  
  1194.   xside = otherside[side];
  1195.   if (kt > kf)
  1196.     {
  1197.       rf = kf+3; rt = kt-1; d = 1;
  1198.     }
  1199.   else
  1200.     {
  1201.       rf = kf-4; rt = kt+1; d = -1;
  1202.     }
  1203.   if (iop == 0)
  1204.     {
  1205.       if (board[kf] != king || board[rf] != rook || color[rf] != side)
  1206.         return(false);
  1207.       if (color[kt] != neutral || color[rt] != neutral) return(false);
  1208.       if (d == -1 && color[kt+d] != neutral) return(false);
  1209.       if (SqAtakd(kf,xside)) return(false);
  1210.       if (SqAtakd(kt,xside)) return(false);
  1211.       if (SqAtakd(kf+d,xside)) return(false);
  1212.     }
  1213.   else
  1214.     {
  1215.       if (iop == 1) castld[side] = true; else castld[side] = false;
  1216.       if (iop == 2)
  1217.         {
  1218.           t0 = kt; kt = kf; kf = t0;
  1219.           t0 = rt; rt = rf; rf = t0;
  1220.         }
  1221.       board[kt] = king; color[kt] = side; Pindex[kt] = 0;
  1222.       board[kf] = no_piece; color[kf] = neutral;
  1223.       board[rt] = rook; color[rt] = side; Pindex[rt] = Pindex[rf];
  1224.       board[rf] = no_piece; color[rf] = neutral;
  1225.       PieceList[side][Pindex[kt]] = kt;
  1226.       PieceList[side][Pindex[rt]] = rt;
  1227.       if (hashflag)
  1228.         {
  1229.           UpdateHashbd(side,king,kf,kt);
  1230.           UpdateHashbd(side,rook,rf,rt);
  1231.         }
  1232.     }
  1233.   return(true);
  1234. }
  1235.  
  1236.  
  1237. EnPassant(xside,f,t,iop)
  1238. short xside,f,t,iop;
  1239.  
  1240. /*
  1241.    Make or unmake an en passant move.
  1242. */
  1243.  
  1244. {
  1245. register short l;
  1246.   if (t > f) l = t-8; else l = t+8;
  1247.   if (iop == 1)
  1248.     {
  1249.       board[l] = no_piece; color[l] = neutral;
  1250.     }
  1251.   else 
  1252.     {
  1253.       board[l] = pawn; color[l] = xside;
  1254.     }
  1255.   InitializeStats();
  1256. }
  1257.  
  1258.  
  1259. MakeMove(side,node,tempb,tempc,tempsf,tempst)
  1260. short side,*tempc,*tempb,*tempsf,*tempst;
  1261. struct leaf *node;
  1262.  
  1263. /*
  1264.    Update Arrays board[], color[], and Pindex[] to reflect the new board 
  1265.    position obtained after making the move pointed to by node. Also 
  1266.    update miscellaneous stuff that changes when a move is made. 
  1267. */
  1268.     
  1269. {
  1270. register short f,t,xside,ct,cf;
  1271.  
  1272.   xside = otherside[side];
  1273.   f = node->f; t = node->t; epsquare = -1;
  1274.   FROMsquare = f; TOsquare = t;
  1275.   INCscore = 0;
  1276.   GameList[++GameCnt].gmove = (f<<8) + t;
  1277.   if (node->flags & cstlmask)
  1278.     {
  1279.       GameList[GameCnt].piece = no_piece;
  1280.       GameList[GameCnt].color = side;
  1281.       castle(side,f,t,1);
  1282.     }
  1283.   else
  1284.     {
  1285.       *tempc = color[t]; *tempb = board[t];
  1286.       *tempsf = svalue[f]; *tempst = svalue[t];
  1287.       GameList[GameCnt].piece = *tempb;
  1288.       GameList[GameCnt].color = *tempc;
  1289.       if (*tempc != neutral)
  1290.         {
  1291.           UpdatePieceList(*tempc,t,1);
  1292.           if (*tempb == pawn) --PawnCnt[*tempc][column[t]];
  1293.           if (board[f] == pawn)
  1294.             {
  1295.               --PawnCnt[side][column[f]];
  1296.               ++PawnCnt[side][column[t]];
  1297.               cf = column[f]; ct = column[t];
  1298.               if (PawnCnt[side][ct] > 1+PawnCnt[side][cf])
  1299.                 INCscore -= 15;
  1300.               else if (PawnCnt[side][ct] < 1+PawnCnt[side][cf])
  1301.                 INCscore += 15;
  1302.               else if (ct == 0 || ct == 7 || PawnCnt[side][ct+ct-cf] == 0)
  1303.                 INCscore -= 15;
  1304.             }
  1305.           mtl[xside] -= value[*tempb];
  1306.           if (*tempb == pawn) pmtl[xside] -= valueP;
  1307.           if (hashflag) UpdateHashbd(xside,*tempb,-1,t);
  1308.           INCscore += *tempst;
  1309.         }
  1310.       color[t] = color[f]; board[t] = board[f]; svalue[t] = svalue[f];
  1311.       Pindex[t] = Pindex[f]; PieceList[side][Pindex[t]] = t;
  1312.       color[f] = neutral; board[f] = no_piece;
  1313.       if (board[t] == pawn)
  1314.         if (t-f == 16) epsquare = f+8;
  1315.         else if (f-t == 16) epsquare = f-8;
  1316.       if (node->flags & promote)
  1317.         {
  1318.           board[t] = queen;
  1319.           --PawnCnt[side][column[t]];
  1320.           mtl[side] += valueQ - valueP;
  1321.           pmtl[side] -= valueP;
  1322.           HasQueen[side] = true;
  1323.           if (hashflag)
  1324.             {
  1325.               UpdateHashbd(side,pawn,f,-1);
  1326.               UpdateHashbd(side,queen,f,-1);
  1327.             }
  1328.           INCscore -= *tempsf;
  1329.         } 
  1330.       if (board[t] == king) ++kingmoved[side];
  1331.       if (node->flags & epmask) EnPassant(xside,f,t,1);
  1332.       else if (hashflag) UpdateHashbd(side,board[t],f,t);
  1333.     }
  1334. }
  1335.  
  1336.  
  1337. UnmakeMove(side,node,tempb,tempc,tempsf,tempst)
  1338. short side,*tempc,*tempb,*tempsf,*tempst;
  1339. struct leaf *node;
  1340.  
  1341. /*
  1342.    Take back a move.
  1343. */
  1344.  
  1345. {
  1346. register short f,t,xside;
  1347.  
  1348.   xside = otherside[side];
  1349.   f = node->f; t = node->t; epsquare = -1;
  1350.   GameCnt--;
  1351.   if (node->flags & cstlmask) castle(side,f,t,2);
  1352.   else
  1353.     {
  1354.       color[f] = color[t]; board[f] = board[t]; svalue[f] = *tempsf;
  1355.       Pindex[f] = Pindex[t]; PieceList[side][Pindex[f]] = f;
  1356.       color[t] = *tempc; board[t] = *tempb; svalue[t] = *tempst;
  1357.       if (node->flags & promote)
  1358.         {
  1359.           board[f] = pawn;
  1360.           ++PawnCnt[side][column[t]];
  1361.           mtl[side] += valueP - valueQ;
  1362.           pmtl[side] += valueP;
  1363.           if (hashflag)
  1364.             {
  1365.               UpdateHashbd(side,queen,-1,t);
  1366.               UpdateHashbd(side,pawn,-1,t);
  1367.             }
  1368.         } 
  1369.       if (*tempc != neutral)
  1370.         {
  1371.           UpdatePieceList(*tempc,t,2);
  1372.           if (*tempb == pawn) ++PawnCnt[*tempc][column[t]];
  1373.           if (board[f] == pawn)
  1374.             {
  1375.               --PawnCnt[side][column[t]];
  1376.               ++PawnCnt[side][column[f]];
  1377.             }
  1378.           mtl[xside] += value[*tempb];
  1379.           if (*tempb == pawn) pmtl[xside] += valueP;
  1380.           if (hashflag) UpdateHashbd(xside,*tempb,-1,t);
  1381.         }
  1382.       if (board[f] == king) --kingmoved[side];
  1383.       if (node->flags & epmask) EnPassant(xside,f,t,2);
  1384.       else if (hashflag) UpdateHashbd(side,board[f],f,t);
  1385.     }
  1386. }
  1387.  
  1388.  
  1389. UpdateHashbd(side,piece,f,t)
  1390. short side,piece,f,t;
  1391.  
  1392. /*
  1393.    hashbd contains a 32 bit "signature" of the board position. hashkey 
  1394.    contains a 16 bit code used to address the hash table. When a move is 
  1395.    made, XOR'ing the hashcode of moved piece on the from and to squares 
  1396.    with the hashbd and hashkey values keeps things current. 
  1397. */
  1398.  
  1399. {
  1400.   if (f >= 0)
  1401.     {
  1402.       hashbd ^= hashcode[side][piece][f].bd;
  1403.       hashkey ^= hashcode[side][piece][f].key;
  1404.     }
  1405.   if (t >= 0)
  1406.     {
  1407.       hashbd ^= hashcode[side][piece][t].bd;
  1408.       hashkey ^= hashcode[side][piece][t].key;
  1409.     }
  1410. }
  1411.  
  1412.  
  1413. UpdatePieceList(side,sq,iop)
  1414. short side,sq,iop;
  1415.  
  1416. /*
  1417.    Update the PieceList and Pindex arrays when a piece is captured or 
  1418.    when a capture is unmade. 
  1419. */
  1420.  
  1421. {
  1422. register short i;
  1423.   if (iop == 1)
  1424.     {
  1425.       PieceCnt[side]--;
  1426.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1427.         {
  1428.           PieceList[side][i] = PieceList[side][i+1];
  1429.           Pindex[PieceList[side][i]] = i;
  1430.         }
  1431.     }
  1432.   else
  1433.     {
  1434.       PieceCnt[side]++;
  1435.       PieceList[side][PieceCnt[side]] = sq;
  1436.       Pindex[sq] = PieceCnt[side];
  1437.     }
  1438. }
  1439.  
  1440.  
  1441. InitializeStats()
  1442.  
  1443. /*
  1444.    Scan thru the board seeing what's on each square. If a piece is found, 
  1445.    update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also 
  1446.    determine the material for each side and set the hashkey and hashbd 
  1447.    variables to represent the current board position. Array 
  1448.    PieceList[side][indx] contains the location of all the pieces of 
  1449.    either side. Array Pindex[sq] contains the indx into PieceList for a 
  1450.    given square. 
  1451. */
  1452.  
  1453. {
  1454. register short i,sq;
  1455.   epsquare = -1;
  1456.   for (i = 0; i < 8; i++)
  1457.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  1458.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  1459.   PieceCnt[white] = PieceCnt[black] = 0;
  1460.   hashbd = hashkey = 0;
  1461.   for (sq = 0; sq < 64; sq++)
  1462.     if (color[sq] != neutral)
  1463.       {
  1464.         mtl[color[sq]] += value[board[sq]];
  1465.         if (board[sq] == pawn)
  1466.           {
  1467.             pmtl[color[sq]] += valueP;
  1468.             ++PawnCnt[color[sq]][column[sq]];
  1469.           }
  1470.         if (board[sq] == king) Pindex[sq] = 0;
  1471.           else Pindex[sq] = ++PieceCnt[color[sq]];
  1472.         PieceList[color[sq]][Pindex[sq]] = sq;
  1473.         hashbd ^= hashcode[color[sq]][board[sq]][sq].bd;
  1474.         hashkey ^= hashcode[color[sq]][board[sq]][sq].key;
  1475.       }
  1476. }
  1477.  
  1478.  
  1479. pick(p1,p2)
  1480. short p1,p2;
  1481.  
  1482. /*  
  1483.    Find the best move in the tree between indexes p1 and p2. Swap the 
  1484.    best move into the p1 element. 
  1485. */
  1486.  
  1487. {
  1488. register short p,s,p0,s0;
  1489. struct leaf temp;
  1490.  
  1491.   s0 = Tree[p1].score; p0 = p1;
  1492.   for (p = p1+1; p <= p2; p++)
  1493.     if ((s = Tree[p].score) > s0)
  1494.       {
  1495.         s0 = s; p0 = p;
  1496.       }
  1497.   if (p0 != p1)
  1498.     {
  1499.       temp = Tree[p1]; Tree[p1] = Tree[p0]; Tree[p0] = temp;
  1500.     }
  1501. }
  1502.  
  1503.  
  1504. repetition(cnt)
  1505. short *cnt;
  1506.  
  1507. /*
  1508.     Check for draw by threefold repetition.
  1509. */
  1510.  
  1511. {
  1512. register short i,c,f,t;
  1513. short b[64];
  1514. unsigned short m;
  1515.   *cnt = c = 0;
  1516.   if (GameCnt > Game50+3)
  1517.     {
  1518.       for (i = 0; i < 64; b[i++] = 0);
  1519.       for (i = GameCnt; i > Game50; i--)
  1520.         {
  1521.           m = GameList[i].gmove; f = m>>8; t = m & 0xFF;
  1522.           if (++b[f] == 0) c--; else c++;
  1523.           if (--b[t] == 0) c--; else c++;
  1524.           if (c == 0) (*cnt)++;
  1525.         }
  1526.     }
  1527. }
  1528.  
  1529.  
  1530. int SqAtakd(sq,side)
  1531. short sq,side;
  1532.  
  1533. /*
  1534.   See if any piece with color 'side' ataks sq.  First check for pawns
  1535.   or king, then try other pieces. Array Dcode is used to check for
  1536.   knight attacks or R,B,Q co-linearity.  
  1537. */
  1538.  
  1539. {
  1540. register short m,d,m0,m1,i,loc,piece,*PL;
  1541.  
  1542.   m1 = map[sq];
  1543.   if (side == white) m = m1-0x0F; else m = m1+0x0F;
  1544.   if (!(m & 0x88))
  1545.     if (board[unmap[m]] == pawn && color[unmap[m]] == side) return(true);
  1546.   if (side == white) m = m1-0x11; else m = m1+0x11;
  1547.   if (!(m & 0x88))
  1548.     if (board[unmap[m]] == pawn && color[unmap[m]] == side) return(true);
  1549.   if (distance(sq,PieceList[side][0]) == 1) return(true);
  1550.   
  1551.   PL = PieceList[side];
  1552.   for (i = 1; i <= PieceCnt[side]; i++)
  1553.     {
  1554.       loc = PL[i]; piece = board[loc];
  1555.       if (piece == pawn) continue;
  1556.       m0 = map[loc]; d = Dcode[abs(m1-m0)];
  1557.       if (d == 0 || (Pdir[d] & pbit[piece]) == 0) continue;
  1558.       if (piece == knight) return(true);
  1559.       else
  1560.         {
  1561.           if (m1 < m0) d = -d;
  1562.           for (m = m0+d; m != m1; m += d)
  1563.             if (color[unmap[m]] != neutral) break;
  1564.           if (m == m1) return(true);
  1565.         }
  1566.     }
  1567.   return(false);
  1568. }
  1569.  
  1570.  
  1571. ataks(side,a)
  1572. short side,*a;
  1573.  
  1574. /*
  1575.     Fill array atak[][] with info about ataks to a square.  Bits 8-15
  1576.     are set if the piece (king..pawn) ataks the square. Bits 0-7
  1577.     contain a count of total ataks to the square.
  1578. */
  1579.  
  1580. {
  1581. register short u,m,d,c,m0;
  1582. short j,j1,j2,piece,i,sq,*PL;
  1583.  
  1584.   for (u = 0; u < 64; a[u++] = 0); 
  1585.   Dstart[pawn] = Dpwn[side]; Dstop[pawn] = Dstart[pawn] + 1;
  1586.   PL = PieceList[side];
  1587.   for (i = 0; i <= PieceCnt[side]; i++)
  1588.     {
  1589.       sq = PL[i];
  1590.       m0 = map[sq];
  1591.       piece = board[sq];
  1592.       c = control[piece]; j1 = Dstart[piece]; j2 = Dstop[piece];
  1593.       if (sweep[piece])
  1594.         for (j = j1; j <= j2; j++)
  1595.           {
  1596.             d = Dir[j]; m = m0+d;
  1597.             while (!(m & 0x88))
  1598.               {
  1599.                 u = unmap[m];
  1600.                 a[u] = ++a[u] | c;
  1601.                 if (color[u] == neutral) m += d;
  1602.                 else break;
  1603.               }
  1604.           }
  1605.       else
  1606.         for (j = j1; j <= j2; j++)
  1607.           if (!((m = m0+Dir[j]) & 0x88))
  1608.             {
  1609.               u = unmap[m];
  1610.               a[u] = ++a[u] | c;
  1611.             }
  1612.     }
  1613. }
  1614.  
  1615.  
  1616. /* ............    POSITIONAL EVALUATION ROUTINES    ............ */
  1617.  
  1618. ScorePosition(side,score)
  1619. short side,*score;
  1620.  
  1621. /*
  1622.    Perform normal static evaluation of board position. A score is 
  1623.    generated for each piece and these are summed to get a score for each 
  1624.    side. 
  1625. */
  1626.  
  1627. {
  1628. register short sq,s,i,xside;
  1629. short pscore[3];
  1630.  
  1631.   wking = PieceList[white][0]; bking = PieceList[black][0];
  1632.   UpdateWeights();
  1633.   xside = otherside[side];
  1634.   pscore[white] = pscore[black] = 0;
  1635.  
  1636.   for (c1 = white; c1 <= black; c1++)
  1637.     {
  1638.       c2 = otherside[c1];
  1639.       if (c1 == white) EnemyKing = bking; else EnemyKing = wking;
  1640.       atk1 = atak[c1]; atk2 = atak[c2];
  1641.       PC1 = PawnCnt[c1]; PC2 = PawnCnt[c2];
  1642.       for (i = 0; i <= PieceCnt[c1]; i++)
  1643.         {
  1644.           sq = PieceList[c1][i];
  1645.           s = SqValue(sq,side);
  1646.           pscore[c1] += s;
  1647.           svalue[sq] = s;
  1648.         }
  1649.     }
  1650.   if (hung[side] > 1) pscore[side] += HUNGX;
  1651.   if (hung[xside] > 1) pscore[xside] += HUNGX;
  1652.   
  1653.   *score = mtl[side] - mtl[xside] + pscore[side] - pscore[xside] + 10;
  1654.   if (dither) *score += rand() % dither;
  1655.   
  1656.   if (*score > 0 && pmtl[side] == 0)
  1657.     if (emtl[side] < valueR) *score = 0;
  1658.     else if (*score < valueR) *score /= 2;
  1659.   if (*score < 0 && pmtl[xside] == 0)
  1660.     if (emtl[xside] < valueR) *score = 0;
  1661.     else if (-*score < valueR) *score /= 2;
  1662.     
  1663.   if (mtl[xside] == valueK && emtl[side] > valueB) *score += 200;
  1664.   if (mtl[side] == valueK && emtl[xside] > valueB) *score -= 200;
  1665. }
  1666.  
  1667.  
  1668. ScoreLoneKing(side,score)
  1669. short side,*score;
  1670.  
  1671. /* 
  1672.    Static evaluation when loser has only a king and winner has no pawns
  1673.    or no pieces.
  1674. */
  1675.  
  1676. {
  1677. register short winner,loser,king1,king2,s,i;
  1678.  
  1679.   UpdateWeights();
  1680.   if (mtl[white] > mtl[black]) winner = white; else winner = black;
  1681.   loser = otherside[winner];
  1682.   king1 = PieceList[winner][0]; king2 = PieceList[loser][0];
  1683.   
  1684.   s = 0;
  1685.   
  1686.   if (pmtl[winner] > 0)
  1687.     for (i = 1; i <= PieceCnt[winner]; i++)
  1688.       s += ScoreKPK(side,winner,loser,king1,king2,PieceList[winner][i]);
  1689.       
  1690.   else if (emtl[winner] == valueB+valueN)
  1691.     s = ScoreKBNK(winner,king1,king2);
  1692.     
  1693.   else if (emtl[winner] > valueB)
  1694.     s = 500 + emtl[winner] - 2*KingEnding[king2] - 2*distance(king1,king2);
  1695.     
  1696.   if (side == winner) *score = s; else *score = -s;
  1697. }
  1698.  
  1699.  
  1700. int ScoreKPK(side,winner,loser,king1,king2,sq)
  1701. short side,winner,loser,king1,king2,sq;
  1702.  
  1703. /*
  1704.    Score King and Pawns versus King endings.
  1705. */
  1706.  
  1707. {
  1708. register short s,r;
  1709.   
  1710.   if (PieceCnt[winner] == 1) s = 50; else s = 120;
  1711.   if (winner == white)
  1712.     {
  1713.       if (side == loser) r = row[sq]-1; else r = row[sq];
  1714.       if (row[king2] >= r && distance(sq,king2) < 8-r) s += 10*row[sq];
  1715.       else s = 500+50*row[sq];
  1716.       if (row[sq] < 6) sq += 16; else sq += 8;
  1717.     }
  1718.   else
  1719.     {
  1720.       if (side == loser) r = row[sq]+1; else r = row[sq];
  1721.       if (row[king2] <= r && distance(sq,king2) < r+1) s += 10*(7-row[sq]);
  1722.       else s = 500+50*(7-row[sq]);
  1723.       if (row[sq] > 1) sq -= 16; else sq -= 8;
  1724.     }
  1725.   s += 8*(taxicab(king2,sq) - taxicab(king1,sq));
  1726.   return(s);
  1727. }
  1728.  
  1729.  
  1730. int ScoreKBNK(winner,king1,king2)
  1731. short winner,king1,king2;
  1732.  
  1733. /*
  1734.    Score King+Bishop+Knight versus King endings.
  1735.    This doesn't work all that well but it's better than nothing.
  1736. */
  1737.  
  1738. {
  1739. register short s;
  1740.   s = emtl[winner] - 300;
  1741.   if (KBNKsq == 0) s += KBNK[king2];
  1742.   else s += KBNK[locn[row[king2]][7-column[king2]]];
  1743.   s -= taxicab(king1,king2);
  1744.   s -= distance(PieceList[winner][1],king2);
  1745.   s -= distance(PieceList[winner][2],king2);
  1746.   return(s);
  1747. }
  1748.  
  1749.  
  1750. SqValue(sq,side)
  1751. short sq,side;
  1752.  
  1753. /*
  1754.    Calculate the positional value for the piece on 'sq'.
  1755. */
  1756.  
  1757. {
  1758. register short j,fyle,rank,a1,a2;
  1759. short s,piece,in_square,r,mob,e,c;
  1760.  
  1761.   piece = board[sq];
  1762.   a1 = (atk1[sq] & 0x4FFF); a2 = (atk2[sq] & 0x4FFF);
  1763.   rank = row[sq]; fyle = column[sq];
  1764.   s = 0;
  1765.   if (piece == pawn && c1 == white)
  1766.     {
  1767.       s = Mwpawn[sq];
  1768.       if (sq == 11 || sq == 12)
  1769.         if (color[sq+8] != neutral) s += PEDRNK2B;
  1770.       if ((fyle == 0 || PC1[fyle-1] == 0) &&
  1771.           (fyle == 7 || PC1[fyle+1] == 0))
  1772.         s += ISOLANI[fyle];
  1773.       else if (PC1[fyle] > 1) s += PDOUBLED;
  1774.       if (a1 < ctlP && atk1[sq+8] < ctlP)
  1775.         {
  1776.           s += BACKWARD[a2 & 0xFF];
  1777.           if (PC2[fyle] == 0) s += PWEAKH;
  1778.           if (color[sq+8] != neutral) s += PBLOK;
  1779.         }
  1780.       if (PC2[fyle] == 0)
  1781.         {
  1782.           if (side == black) r = rank-1; else r = rank;
  1783.           in_square = (row[bking] >= r && distance(sq,bking) < 8-r);
  1784.           if (a2 == 0 || side == white) e = 0; else e = 1;
  1785.           for (j = sq+8; j < 64; j += 8)
  1786.             if (atk2[j] >= ctlP) { e = 2; break; }
  1787.             else if (atk2[j] > 0 || color[j] != neutral) e = 1;
  1788.           if (e == 2) s += (stage*PassedPawn3[rank]) / 10;
  1789.           else if (in_square || e == 1) s += (stage*PassedPawn2[rank]) / 10;
  1790.           else if (emtl[black] > 0) s += (stage*PassedPawn1[rank]) / 10;
  1791.           else s += PassedPawn0[rank];
  1792.         }
  1793.     }
  1794.   else if (piece == pawn && c1 == black)
  1795.     {
  1796.       s = Mbpawn[sq];
  1797.       if (sq == 51 || sq == 52)
  1798.         if (color[sq-8] != neutral) s += PEDRNK2B;
  1799.       if ((fyle == 0 || PC1[fyle-1] == 0) &&
  1800.           (fyle == 7 || PC1[fyle+1] == 0))
  1801.         s += ISOLANI[fyle];
  1802.       else if (PC1[fyle] > 1) s += PDOUBLED;
  1803.       if (a1 < ctlP && atk1[sq-8] < ctlP)
  1804.         {
  1805.           s += BACKWARD[a2 & 0xFF];
  1806.           if (PC2[fyle] == 0) s += PWEAKH;
  1807.           if (color[sq-8] != neutral) s += PBLOK;
  1808.         }
  1809.       if (PC2[fyle] == 0)
  1810.         {
  1811.           if (side == white) r = rank+1; else r = rank;
  1812.           in_square = (row[wking] <= r && distance(sq,wking) < r+1);
  1813.           if (a2 == 0 || side == black) e = 0; else e = 1;
  1814.           for (j = sq-8; j >= 0; j -= 8)
  1815.             if (atk2[j] >= ctlP) { e = 2; break; }
  1816.             else if (atk2[j] > 0 || color[j] != neutral) e = 1;
  1817.           if (e == 2) s += (stage*PassedPawn3[7-rank]) / 10;
  1818.           else if (in_square || e == 1) s += (stage*PassedPawn2[7-rank]) / 10;
  1819.           else if (emtl[white] > 0) s += (stage*PassedPawn1[7-rank]) / 10;
  1820.           else s += PassedPawn0[7-rank];
  1821.         }
  1822.     }
  1823.   else if (piece == knight)
  1824.     {
  1825.       s = Mknight[c1][sq];
  1826.     }
  1827.   else if (piece == bishop)
  1828.     {
  1829.       s = Mbishop[c1][sq];
  1830.       BRscan(sq,&s,&mob);
  1831.       s += BMBLTY[mob];
  1832.     }
  1833.   else if (piece == rook)
  1834.     {
  1835.       s += RookBonus;
  1836.       BRscan(sq,&s,&mob);
  1837.       s += RMBLTY[mob];
  1838.       if (PC1[fyle] == 0) s += RHOPN;
  1839.       if (PC2[fyle] == 0) s += RHOPNX;
  1840.       if (rank == rank7[c1] && pmtl[c2] > 100) s += 10;
  1841.       if (stage > 2) s += 14 - taxicab(sq,EnemyKing);
  1842.     }
  1843.   else if (piece == queen)
  1844.     {
  1845.       if (stage > 2) s += 14 - taxicab(sq,EnemyKing);
  1846.       if (distance(sq,EnemyKing) < 3) s += 12;
  1847.     }
  1848.   else if (piece == king)
  1849.     {
  1850.       s = Mking[c1][sq];
  1851.       if (KSFTY > 0)
  1852.         if (Developed[c2] || stage > 0) KingScan(sq,&s);
  1853.       if (castld[c1]) s += KCASTLD;
  1854.       else if (kingmoved[c1]) s += KMOVD;
  1855.  
  1856.       if (PC1[fyle] == 0) s += KHOPN;
  1857.       if (PC2[fyle] == 0) s += KHOPNX;
  1858.       if (fyle == 1 || fyle == 2 || fyle == 3 || fyle == 7)
  1859.         {
  1860.           if (PC1[fyle-1] == 0) s += KHOPN;
  1861.           if (PC2[fyle-1] == 0) s += KHOPNX;
  1862.         }
  1863.       if (fyle == 4 || fyle == 5 || fyle == 6 || fyle == 0)
  1864.         {
  1865.           if (PC1[fyle+1] == 0) s += KHOPN;
  1866.           if (PC2[fyle+1] == 0) s += KHOPNX;
  1867.         }
  1868.       if (fyle == 2)
  1869.         {
  1870.           if (PC1[0] == 0) s += KHOPN;
  1871.           if (PC2[0] == 0) s += KHOPNX;
  1872.         }
  1873.       if (fyle == 5)
  1874.         {
  1875.           if (PC1[7] == 0) s += KHOPN;
  1876.           if (PC2[7] == 0) s += KHOPNX;
  1877.         }
  1878.     }
  1879.     
  1880.   if (a2 > 0) 
  1881.     {
  1882.       c = (control[piece] & 0x4FFF);
  1883.       if (a1 == 0 || a2 > c+1)
  1884.         {
  1885.           s += HUNGP;
  1886.           ++hung[c1];
  1887.           if (piece != king && trapped(sq,piece)) ++hung[c1];
  1888.         }
  1889.       else if (piece != pawn || a2 > a1)
  1890.         if (a2 >= c || a1 < ctlP) s += ATAKD;
  1891.     }
  1892.   return(s);
  1893. }
  1894.  
  1895.  
  1896. KingScan(sq,s)
  1897. short sq,*s;
  1898.  
  1899. /*
  1900.    Assign penalties if king can be threatened by checks, if squares
  1901.    near the king are controlled by the enemy (especially the queen),
  1902.    or if there are no pawns near the king.
  1903. */
  1904.  
  1905. #define ScoreThreat\
  1906.   if (color[u] != c2)\
  1907.     if (atk1[u] == 0 || (atk2[u] & 0xFF) > 1) ++cnt;\
  1908.     else *s -= 3
  1909.  
  1910. {
  1911. register short m,u,d,i,m0,cnt,ok;
  1912.  
  1913.   cnt = 0;
  1914.   m0 = map[sq];
  1915.   if (HasBishop[c2] || HasQueen[c2])
  1916.     for (i = Dstart[bishop]; i <= Dstop[bishop]; i++)
  1917.       {
  1918.         d = Dir[i]; m = m0+d;
  1919.         while (!(m & 0x88))
  1920.           {
  1921.             u = unmap[m];
  1922.             if (atk2[u] & ctlBQ) ScoreThreat;
  1923.             if (color[u] != neutral) break;
  1924.             m += d;
  1925.           }
  1926.       }
  1927.   if (HasRook[c2] || HasQueen[c2])
  1928.     for (i = Dstart[rook]; i <= Dstop[rook]; i++)
  1929.       {
  1930.         d = Dir[i]; m = m0+d;
  1931.         while (!(m & 0x88))
  1932.           {
  1933.             u = unmap[m];
  1934.             if (atk2[u] & ctlRQ) ScoreThreat;
  1935.             if (color[u] != neutral) break;
  1936.             m += d;
  1937.           }
  1938.       }
  1939.   if (HasKnight[c2])
  1940.     for (i = Dstart[knight]; i <= Dstop[knight]; i++)
  1941.       if (!((m = m0+Dir[i]) & 0x88))
  1942.         {
  1943.           u = unmap[m];
  1944.           if (atk2[u] & ctlNN) ScoreThreat;
  1945.         }
  1946.   *s += (KSFTY*Kthreat[cnt]) / 16;
  1947.  
  1948.   cnt = 0; ok = false;
  1949.   m0 = map[sq];
  1950.   for (i = Dstart[king]; i <= Dstop[king]; i++)
  1951.     if (!((m = m0+Dir[i]) & 0x88))
  1952.       {
  1953.         u = unmap[m];
  1954.         if (board[u] == pawn) ok = true;
  1955.         if (atk2[u] > atk1[u])
  1956.           {
  1957.             ++cnt;
  1958.             if (atk2[u] & ctlQ)
  1959.               if (atk2[u] > ctlQ+1 && atk1[u] < ctlQ) *s -= 4*KSFTY;
  1960.           }
  1961.       }
  1962.   if (!ok) *s -= KSFTY;
  1963.   if (cnt > 1) *s -= KSFTY;
  1964. }
  1965.  
  1966.  
  1967. BRscan(sq,s,mob)
  1968. short sq,*s,*mob;
  1969.  
  1970. /*
  1971.    Find Bishop and Rook mobility, XRAY attacks, and pins. Increment the 
  1972.    hung[] array if a pin is found. 
  1973. */
  1974.  
  1975. {
  1976. register short m,u,d,m0,j,piece,pin;
  1977. short *Kf; 
  1978.  
  1979.   Kf = Kfield[c1];
  1980.   *mob = 0;
  1981.   m0 = map[sq]; piece = board[sq];
  1982.   for (j = Dstart[piece]; j <= Dstop[piece]; j++)
  1983.     {
  1984.       pin = -1;
  1985.       d = Dir[j]; m = m0+d;
  1986.       while (!(m & 0x88))
  1987.         {
  1988.           u = unmap[m]; *s += Kf[u];
  1989.           if (color[u] == neutral)
  1990.             {
  1991.               (*mob)++;
  1992.               m += d;
  1993.             }
  1994.           else if (pin < 0)
  1995.             {
  1996.               if (board[u] == pawn || board[u] == king) break;
  1997.               pin = u;
  1998.               m += d;
  1999.             }
  2000.           else if (color[u] == c2 && (board[u] > piece || atk2[u] == 0))
  2001.             {
  2002.               if (color[pin] == c2)
  2003.                 {
  2004.                   *s += PINVAL;
  2005.                   if (atk2[pin] == 0 ||
  2006.                       atk1[pin] > control[board[pin]]+1)
  2007.                     ++hung[c2];
  2008.                 }
  2009.               else *s += XRAY;
  2010.               break;
  2011.             }
  2012.           else break;
  2013.         }
  2014.     }
  2015. }
  2016.  
  2017.  
  2018. int trapped(sq,piece)
  2019. short sq,piece;
  2020.  
  2021. /*
  2022.    See if the attacked piece has unattacked squares to move to.
  2023. */
  2024.  
  2025. {
  2026. register short u,m,d,i,m0;
  2027.  
  2028.   m0 = map[sq];
  2029.   if (sweep[piece])
  2030.     for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  2031.       {
  2032.         d = Dir[i]; m = m0+d;
  2033.         while (!(m & 0x88))
  2034.           {
  2035.             u = unmap[m];
  2036.             if (color[u] == c1) break;
  2037.             if (atk2[u] == 0 || board[u] >= piece) return(false);
  2038.             if (color[u] == c2) break;
  2039.             m += d;
  2040.           }
  2041.       }
  2042.   else if (piece == pawn)
  2043.     {
  2044.       if (c1 == white) u = sq+8; else u = sq-8;
  2045.       if (color[u] == neutral && atk1[u] >= atk2[u])
  2046.         return(false);
  2047.       if (!((m = m0+Dir[Dpwn[c1]]) & 0x88))
  2048.         if (color[unmap[m]] == c2) return(false);
  2049.       if (!((m = m0+Dir[Dpwn[c1]+1]) & 0x88))
  2050.         if (color[unmap[m]] == c2) return(false);
  2051.     }
  2052.   else
  2053.     {
  2054.       for (i = Dstart[piece]; i <= Dstop[piece]; i++)
  2055.         if (!((m = m0+Dir[i]) & 0x88))
  2056.           {
  2057.             u = unmap[m];
  2058.             if (color[u] != c1)
  2059.               if (atk2[u] == 0 || board[u] >= piece) return(false);
  2060.           }
  2061.     }
  2062.   return(true);
  2063. }
  2064.  
  2065.  
  2066. ExaminePosition()
  2067.  
  2068. /*
  2069.    This is done one time before the search is started. Set up arrays 
  2070.    Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the 
  2071.    SqValue() function to determine the positional value of each piece. 
  2072. */
  2073.  
  2074. {
  2075. register short i,sq;
  2076. short wpadv,bpadv,wstrong,bstrong,z,side,pp,j,val,Pd,fyle,rank;
  2077.  
  2078.   wking = PieceList[white][0]; bking = PieceList[black][0];
  2079.   ataks(white,atak[white]); ataks(black,atak[black]);
  2080.   Zwmtl = Zbmtl = 0;
  2081.   UpdateWeights();
  2082.   HasPawn[white] = HasPawn[black] = 0;
  2083.   HasKnight[white] = HasKnight[black] = 0;
  2084.   HasBishop[white] = HasBishop[black] = 0;
  2085.   HasRook[white] = HasRook[black] = 0;
  2086.   HasQueen[white] = HasQueen[black] = 0;
  2087.   for (side = white; side <= black; side++)
  2088.     for (i = 0; i <= PieceCnt[side]; i++)
  2089.       switch (board[PieceList[side][i]])
  2090.         {
  2091.           case pawn : ++HasPawn[side]; break;
  2092.           case knight : ++HasKnight[side]; break;
  2093.           case bishop : ++HasBishop[side]; break;
  2094.           case rook : ++HasRook[side]; break;
  2095.           case queen : ++HasQueen[side]; break;
  2096.         }
  2097.   if (!Developed[white])
  2098.     Developed[white] = (board[1] != knight && board[2] != bishop &&
  2099.                         board[5] != bishop && board[6] != knight);
  2100.   if (!Developed[black])
  2101.     Developed[black] = (board[57] != knight && board[58] != bishop &&
  2102.                         board[61] != bishop && board[62] != knight);
  2103.   if (!PawnStorm && stage < 5)
  2104.     PawnStorm = ((column[wking] < 3 && column[bking] > 4) ||
  2105.                  (column[wking] > 4 && column[bking] < 3));
  2106.   
  2107.   CopyBoard(pknight,Mknight[white]);
  2108.   CopyBoard(pknight,Mknight[black]);
  2109.   CopyBoard(pbishop,Mbishop[white]);
  2110.   CopyBoard(pbishop,Mbishop[black]);
  2111.   BlendBoard(KingOpening,KingEnding,Mking[white]);
  2112.   BlendBoard(KingOpening,KingEnding,Mking[black]);
  2113.   
  2114.   for (sq = 0; sq < 64; sq++)
  2115.     {
  2116.       fyle = column[sq]; rank = row[sq];
  2117.       wstrong = bstrong = true;
  2118.       for (i = sq; i < 64; i += 8)
  2119.         if (atak[black][i] >= ctlP) wstrong = false;
  2120.       for (i = sq; i >= 0; i -= 8)
  2121.         if (atak[white][i] >= ctlP) bstrong = false;
  2122.       wpadv = bpadv = PADVNCM;
  2123.       if ((fyle == 0 || PawnCnt[white][fyle-1] == 0) &&
  2124.           (fyle == 7 || PawnCnt[white][fyle+1] == 0)) wpadv = PADVNCI;
  2125.       if ((fyle == 0 || PawnCnt[black][fyle-1] == 0) &&
  2126.           (fyle == 7 || PawnCnt[black][fyle+1] == 0)) bpadv = PADVNCI;
  2127.       Mwpawn[sq] = (wpadv*PawnAdvance[sq]) / 10;
  2128.       Mbpawn[sq] = (bpadv*PawnAdvance[63-sq]) / 10;
  2129.       Mwpawn[sq] += PawnBonus; Mbpawn[sq] += PawnBonus;
  2130.       if (castld[white] || kingmoved[white])
  2131.         {
  2132.           if ((fyle < 3 || fyle > 4) && distance(sq,wking) < 3)
  2133.             Mwpawn[sq] += PAWNSHIELD;
  2134.         }
  2135.       else if (rank < 3 && (fyle < 2 || fyle > 5))
  2136.         Mwpawn[sq] += PAWNSHIELD / 2;
  2137.       if (castld[black] || kingmoved[black])
  2138.         {
  2139.           if ((fyle < 3 || fyle > 4) && distance(sq,bking) < 3)
  2140.             Mbpawn[sq] += PAWNSHIELD;
  2141.         }
  2142.       else if (rank > 4 && (fyle < 2 || fyle > 5))
  2143.         Mbpawn[sq] += PAWNSHIELD / 2;
  2144.       if (PawnStorm)
  2145.         {
  2146.           if ((column[wking] < 4 && fyle > 4) ||
  2147.               (column[wking] > 3 && fyle < 3)) Mwpawn[sq] += 3*rank - 21;
  2148.           if ((column[bking] < 4 && fyle > 4) ||
  2149.               (column[bking] > 3 && fyle < 3)) Mbpawn[sq] -= 3*rank;
  2150.         }
  2151.         
  2152.       Mknight[white][sq] += 5 - distance(sq,bking);
  2153.       Mknight[white][sq] += 5 - distance(sq,wking);
  2154.       Mknight[black][sq] += 5 - distance(sq,wking);
  2155.       Mknight[black][sq] += 5 - distance(sq,bking);
  2156.       Mbishop[white][sq] += BishopBonus;
  2157.       Mbishop[black][sq] += BishopBonus;
  2158.       for (i = 0; i <= PieceCnt[black]; i++)
  2159.         if (distance(sq,PieceList[black][i]) < 3)
  2160.           Mknight[white][sq] += KNIGHTPOST;
  2161.       for (i = 0; i <= PieceCnt[white]; i++)
  2162.         if (distance(sq,PieceList[white][i]) < 3)
  2163.           Mknight[black][sq] += KNIGHTPOST;
  2164.       if (wstrong) Mknight[white][sq] += KNIGHTSTRONG;
  2165.       if (bstrong) Mknight[black][sq] += KNIGHTSTRONG;
  2166.       if (wstrong) Mbishop[white][sq] += BISHOPSTRONG;
  2167.       if (bstrong) Mbishop[black][sq] += BISHOPSTRONG;
  2168.       
  2169.       if (HasBishop[white] == 2) Mbishop[white][sq] += 8;
  2170.       if (HasBishop[black] == 2) Mbishop[black][sq] += 8;
  2171.       if (HasKnight[white] == 2) Mknight[white][sq] += 5;
  2172.       if (HasKnight[black] == 2) Mknight[black][sq] += 5;
  2173.       
  2174.       if (board[sq] == bishop)
  2175.         if (rank % 2 == fyle % 2) KBNKsq = 0; else KBNKsq = 7;
  2176.         
  2177.       Kfield[white][sq] = Kfield[black][sq] = 0;
  2178.       if (distance(sq,wking) == 1) Kfield[black][sq] = KATAK;
  2179.       if (distance(sq,bking) == 1) Kfield[white][sq] = KATAK;
  2180.       
  2181.       Pd = 0;
  2182.       for (i = 0; i < 64; i++)
  2183.         if (board[i] == pawn)
  2184.           {
  2185.             if (color[i] == white)
  2186.               {
  2187.                 pp = true;
  2188.                 if (row[i] == 6) z = i+8; else z = i+16;
  2189.                 for (j = i+8; j < 64; j += 8)
  2190.                   if (atak[black][j] > ctlP || board[j] == pawn) pp = false;
  2191.               }
  2192.             else
  2193.               {
  2194.                 pp = true;
  2195.                 if (row[i] == 1) z = i-8; else z = i-16;
  2196.                 for (j = i-8; j >= 0; j -= 8)
  2197.                   if (atak[white][j] > ctlP || board[j] == pawn) pp = false;
  2198.               }
  2199.             if (pp) Pd += 5*taxicab(sq,z); else Pd += taxicab(sq,z);
  2200.           }
  2201.       if (Pd != 0)
  2202.         {
  2203.           val = (Pd*stage2) / 10;
  2204.           Mking[white][sq] -= val;
  2205.           Mking[black][sq] -= val;
  2206.         }
  2207.     }
  2208. }
  2209.  
  2210.  
  2211. UpdateWeights()
  2212.  
  2213. /* 
  2214.    If material balance has changed, determine the values for the 
  2215.    positional evaluation terms. 
  2216. */
  2217.  
  2218. {
  2219. register short tmtl;
  2220.  
  2221.   if (mtl[white] != Zwmtl || mtl[black] != Zbmtl)
  2222.     {
  2223.       Zwmtl = mtl[white]; Zbmtl = mtl[black];
  2224.       emtl[white] = Zwmtl - pmtl[white] - valueK;
  2225.       emtl[black] = Zbmtl - pmtl[black] - valueK;
  2226.       tmtl = emtl[white] + emtl[black];
  2227.       if (tmtl > 6600) stage = 0;
  2228.       else if (tmtl < 1400) stage = 10;
  2229.       else stage = (6600-tmtl) / 520;
  2230.       if (tmtl > 3600) stage2 = 0;
  2231.       else if (tmtl < 1400) stage2 = 10;
  2232.       else stage2 = (3600-tmtl) / 220;
  2233.       
  2234.       PEDRNK2B = -15;         /* centre pawn on 2nd rank & blocked */
  2235.       PBLOK = -4;             /* blocked backward pawn */
  2236.       PDOUBLED = -14;         /* doubled pawn */
  2237.       PWEAKH  = -4;           /* weak pawn on half open file */
  2238.       PAWNSHIELD = 10-stage;  /* pawn near friendly king */
  2239.       PADVNCM =  10;          /* advanced pawn multiplier */
  2240.       PADVNCI = 7;            /* muliplier for isolated pawn */
  2241.       PawnBonus = stage;
  2242.       
  2243.       KNIGHTPOST = (stage+2)/3;   /* knight near enemy pieces */
  2244.       KNIGHTSTRONG = (stage+6)/2; /* occupies pawn hole */
  2245.       
  2246.       BISHOPSTRONG = (stage+6)/2; /* occupies pawn hole */
  2247.       BishopBonus = 2*stage;
  2248.       
  2249.       RHOPN    = 10;          /* rook on half open file */
  2250.       RHOPNX   = 4;
  2251.       RookBonus = 6*stage;
  2252.       
  2253.       XRAY     = 8;           /* Xray attack on piece */
  2254.       PINVAL   = 10;          /* Pin */
  2255.       
  2256.       KHOPN    = (3*stage-30) / 2; /* king on half open file */
  2257.       KHOPNX   = KHOPN / 2;
  2258.       KCASTLD  = 10 - stage;
  2259.       KMOVD    = -40 / (stage+1);  /* king moved before castling */
  2260.       KATAK    = (10-stage) / 2;   /* B,R attacks near enemy king */
  2261.       if (stage < 8) KSFTY = 16-2*stage; else KSFTY = 0;
  2262.       
  2263.       ATAKD    = -6;          /* defender > attacker */
  2264.       HUNGP    = -8;          /* each hung piece */
  2265.       HUNGX    = -12;         /* extra for >1 hung piece */
  2266.     }
  2267. }
  2268.  
  2269.  
  2270. int distance(a,b)
  2271. short a,b;
  2272. {
  2273. register short d1,d2;
  2274.  
  2275.   d1 = abs(column[a]-column[b]);
  2276.   d2 = abs(row[a]-row[b]);
  2277.   if (d1 > d2) return(d1); else return(d2);
  2278. }
  2279.  
  2280.  
  2281. BlendBoard(a,b,c)
  2282. short a[64],b[64],c[64];
  2283. {
  2284. register int sq;
  2285.   for (sq = 0; sq < 64; sq++)
  2286.     c[sq] = (a[sq]*(10-stage) + b[sq]*stage) / 10;
  2287. }
  2288.  
  2289.  
  2290. CopyBoard(a,b)
  2291. short a[64],b[64];
  2292. {
  2293. register int sq;
  2294.   for (sq = 0; sq < 64; sq++)
  2295.     b[sq] = a[sq];
  2296. }
  2297.  
  2298. #ifdef MEGAMAX
  2299. /*
  2300.   All this time() does is read the 200Hz clock. That's good enough for
  2301.   what we need (keeping track of elapsed time).
  2302. */
  2303.  
  2304. static long ticks;
  2305.  
  2306. static getticks() {
  2307.     ticks = *((long *) 0x4ba);
  2308. }
  2309.  
  2310. long time()
  2311. {
  2312.     Supexec(getticks);    
  2313.     return ticks/200L;
  2314. }
  2315.  
  2316. int kbhit()
  2317. {
  2318.     if (Cconis) {
  2319.         Cconin();
  2320.         return 1;
  2321.     }
  2322.     return 0;
  2323. }
  2324.  
  2325. access(filnam)
  2326. char *filnam;
  2327. {
  2328.     FILE *f;
  2329.     if (!(f = fopen(filnam, "w")))
  2330.         return -1;
  2331.     fclose(f);
  2332.     return 1;
  2333. }
  2334. #endif
  2335.